<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.3, maximum-scale=1.0, user-scalable=0,shrink-to-fit=no, viewport-fit=cover">
    <meta name="keywords" content="蓝桥到家">
    <meta name="description" itemprop="description" content="生鲜副食、饮品乳品、鲜花蛋糕">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="../styles/reset.css" />
    <link rel="stylesheet" type="text/css" href="../styles/login.css">
    <link rel="stylesheet" type="text/css" href="../styles/common.css" />
    <script src="../js/common.js"></script>
    <script src="../js/zepto.min.js"></script>
</head>
<body>
    <div class="wrap">
        <div class="container">
            <div class="header_logo">
                <img src="https://static.shiyanlou.com/lanqiao/frontend/dist/img/lanqiao-logo.1bbdf85.svg" alt="logo">
            </div>

            <ul class="login_tab">
                <li class="cur"><span>验证码登录</span></li>
                <li><span>密码登录</span></li>
            </ul>

            <ul class="login_show">
                <!-- 验证码登录 -->
                <li class="cur">
                    <div class="login_item">
                        <span class="login_text">手机号</span>
                        <input class="login_input phone-code" type="tel" maxlength="11" placeholder="输入手机号" />
                    </div>
                    <div class="login_item">
                        <span class="login_text">验证码</span>
                        <div class="auth_div">
                            <input class="login_input code-code" type="text" maxlength="4" placeholder="输入验证码" />
                            <div class="code" id="send-code" style="background-color: #007bff; color: white; padding: 5px; text-align: center; cursor: pointer;">
                                <span>获取验证码</span>
                            </div>
                        </div>
                    </div>
                    <div class="login_button">
                        <a href="javascript:;" class="btn-login-code"><span>登录</span></a>
                    </div>
                </li>

                <!-- 密码登录 -->
                <li>
                    <div class="login_item">
                        <span class="login_text">手机号</span>
                        <input class="login_input phone-pwd" type="tel" maxlength="11" placeholder="输入手机号" />
                    </div>
                    <div class="login_item">
                        <span class="login_text">密码</span>
                        <input class="login_input pwd-pwd" type="password" maxlength="12" placeholder="输入密码" />
                    </div>
                    <div class="login_button">
                        <a href="javascript:;" class="btn-login-pwd"><span>登录</span></a>
                    </div>
                </li>
            </ul>

            <div class="register_div">
                <a class="register_text" href="register.html">注册账号</a>
            </div>
        </div>
    </div>

    <script>
        $(function () {

            // 切换登录方式
            $('.login_tab li').click(function () {
                if ($(this).hasClass('cur')) return;
                $(this).addClass('cur').siblings().removeClass('cur');
                var i = $(this).index();
                $('.login_show > li').eq(i).addClass('cur').siblings().removeClass('cur');
            });

            // 生成随机验证码
            function generateRandomCode() {
                return Math.floor(1000 + Math.random() * 9000).toString();
            }

            // 获取验证码按钮点击事件
            $('#send-code').on('click', function () {
                const phone = $('.phone-code').val().trim();
                if (!phone) return alert('请输入手机号');

                // 生成随机验证码
                const randomCode = generateRandomCode();

                // 模拟发送验证码
                alert('验证码已发送，请注意查收，验证码为：' + randomCode);

                // 按钮变色
                $(this).css('background-color', '#ccc');
                $(this).css('cursor', 'default');
                $(this).find('span').text('60秒后重试');

                // 禁用按钮
                $(this).off('click');

                // 倒计时
                let countdown = 60;
                let countdownInterval = setInterval(function () {
                    countdown--;
                    if (countdown <= 0) {
                        clearInterval(countdownInterval);
                        $('#send-code').css('background-color', '#007bff');
                        $('#send-code').css('cursor', 'pointer');
                        $('#send-code').find('span').text('获取验证码');
                        // 重新启用按钮
                        $('#send-code').on('click', function () {
                            const phone = $('.phone-code').val().trim();
                            if (!phone) return alert('请输入手机号');

                            // 生成新的随机验证码
                            const newRandomCode = generateRandomCode();

                            // 模拟发送验证码
                            alert('验证码已发送，请注意查收，验证码为：' + newRandomCode);

                            // 按钮变色
                            $(this).css('background-color', '#ccc');
                            $(this).css('cursor', 'default');
                            $(this).find('span').text('60秒后重试');

                            // 禁用按钮
                            $(this).off('click');

                            // 倒计时
                            countdown = 60;
                            countdownInterval = setInterval(function () {
                                countdown--;
                                if (countdown <= 0) {
                                    clearInterval(countdownInterval);
                                    $('#send-code').css('background-color', '#007bff');
                                    $('#send-code').css('cursor', 'pointer');
                                    $('#send-code').find('span').text('获取验证码');
                                    // 重新启用按钮
                                    $('#send-code').on('click', arguments.callee);
                                } else {
                                    $('#send-code').find('span').text(countdown + '秒后重试');
                                }
                            }, 1000);
                        });
                    } else {
                        $('#send-code').find('span').text(countdown + '秒后重试');
                    }
                }, 1000);
            });

            // 验证码登录
            $('.btn-login-code').on('click', function () {
                const phone = $('.phone-code').val().trim();
                const codeInput = $('.code-code').val().trim();

                if (!phone || !codeInput) return alert('请输入手机号和验证码');

                // 检查验证码是否正确（这里假设验证码是随机生成的，实际中应从服务器获取）
                $.getJSON('http://localhost:3000/users?phone=' + phone, function (users) {
                    if (users.length > 0) {
                        alert('登录成功');
                        location.href = '../index.html';
                    } else {
                        alert('该手机号未注册，请先注册');
                        location.href = 'register.html';
                    }
                });
            });

            // 密码登录
            $('.btn-login-pwd').on('click', function () {
                const phone = $('.phone-pwd').val().trim();
                const password = $('.pwd-pwd').val().trim();

                if (!phone || !password) return alert('请输入手机号和密码');

                $.getJSON('http://localhost:3000/users?phone=' + phone, function (users) {
                    if (users.length > 0 && users[0].password === password) {
                        alert('登录成功');
                        location.href = '../index.html';
                    } else if (users.length === 0) {
                        alert('该手机号未注册，请先注册');
                        location.href = 'register.html';
                    } else {
                        alert('密码错误');
                    }
                });
            });
        });
    </script>
</body>
</html>